package org.hwadee.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.hwadee.entity.Category;
import org.hwadee.entity.Post;
import org.hwadee.entity.User;
import org.hwadee.mapper.CategoryMapper;
import org.hwadee.mapper.CommentMapper;
import org.hwadee.mapper.PostMapper;
import org.hwadee.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PostService {

    @Autowired
    PostMapper postMapper;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    UserMapper userMapper;


    public void deletePostByCategory(Category category) {
        postMapper.deletePostByCategory(category);
    }

    public List<Post> getAllPosts() {
        return postMapper.getAllPostss();
    }

    public List<Post> getPostsByUser(User user) {
        return postMapper.getPostsByUser(user);
    }

    public void insertPost(Post post) {
        postMapper.insertPost(post);
    }

    public Post getPostByIdAndStatus(Integer id, int i) {
        return postMapper.getPostByIdAndStatus(id, i);
    }

    public void deletePost(Post post) {
        postMapper.deletePost(post);
    }

    public void updatePost(Post post) {
        postMapper.updatePost(post);
    }

    public List<Post> listPostByCategoryId(Integer categoryId, int i) {
        return postMapper.listPostByCategoryId(categoryId, i);
    }

    public List<Post> listPostByCategoryId(Integer categoryId) {
        return postMapper.listPostOnlyByCategoryId(categoryId);
    }

    public List<Post> listPostByViewCount(int i) {
        return postMapper.listPostByViewCount(i);
    }

    public Post getAfterPost(Integer postId) {
        return postMapper.getAfterPost(postId);
    }

    public Post getPrePost(Integer postId) {
        return postMapper.getPrePost(postId);
    }

    public List<Post> listRandomPost(int i) {
        return postMapper.listRandomPost(i);
    }

    public List<Post> listPostByCommentCount(int i) {
        return postMapper.listPostByCommentCount(i);
    }

    public void updatePostLike(Post post) {
        postMapper.updatePostLike(post);
    }

    public void updatePostView(Post post) {
        postMapper.updatePostView(post);
    }

    public List<Post> getPostsByKeywords(String keywords) {
        return postMapper.getPostsByKeywords("%" + keywords + "%");
    }

    public List<Post> listRecentPost(int limit) {
        return postMapper.listRecentPost(limit);
    }

    public List<Post> listRecentPostById(Integer userId, int i) {
        return postMapper.listRecentPostById(userId,i);
    }

    public List<Post> getPostsByCategoryId(Integer categoryId) {
        return postMapper.getPostsByCategoryId(categoryId);
    }

    public void updateCommentCount(Integer postId) {
        Integer commentCount = commentMapper.countCommentByPostId(postId);
        postMapper.updateCommentCount(postId, commentCount);
    }

    public PageInfo<Post> pagePost(Integer pageIndex, Integer pageSize, User user) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Post> postList = postMapper.getPostsByUser(user);
        for (int i = 0; i < postList.size(); i++) {
            //封装CategoryList
            Category category = categoryMapper.getCategoryById(postMapper.getCategoryIdByPostId(postList.get(i).getPostId()));

            postList.get(i).setCat(category);

            postList.get(i).setUser(userMapper.getUserById(postList.get(i).getPostUserId()));
        }
        return new PageInfo<>(postList);
    }

    public Integer getCategoryIdByPostId(Integer postId) {
        return postMapper.getCategoryIdByPostId(postId);
    }

    public PageInfo<Post> pagePostAll(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Post> postList = postMapper.getAllPostss();
        for (int i = 0; i < postList.size(); i++) {
            //封装Category
            Category category = categoryMapper.getCategoryById(postMapper.getCategoryIdByPostId(postList.get(i).getPostId()));

            postList.get(i).setCat(category);

            postList.get(i).setUser(userMapper.getUserById(postList.get(i).getPostUserId()));
        }
        return new PageInfo<>(postList);
    }

    public PageInfo<Post> pageNotice(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Post> postList = postMapper.getAllNotices();
        for (int i = 0; i < postList.size(); i++) {
            //封装Category
            Category category = categoryMapper.getCategoryById(postMapper.getCategoryIdByPostId(postList.get(i).getPostId()));

            postList.get(i).setCat(category);

            postList.get(i).setUser(userMapper.getUserById(postList.get(i).getPostUserId()));
        }
        return new PageInfo<>(postList);
    }


    public List<Post> getAllNotices() {
        List<Post> postList = postMapper.getAllNotices();
        for (int i = 0; i < postList.size(); i++) {
            postList.get(i).setUser(userMapper.getUserById(postList.get(i).getPostUserId()));
        }
        return postList;
    }

    public void deletePostById(Integer id) {
        postMapper.deletePostById(id);
    }

    public void recoveryPostById(Integer id) {
        postMapper.recoveryPostById(id);
    }

    public PageInfo<Post> pageSearchPost(Integer pageIndex, Integer pageSize, User user, String search) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Post> postList = postMapper.getPostsByUserAndSearch(user.getUserId(), "%" + search + "%");
        for (int i = 0; i < postList.size(); i++) {
            //封装CategoryList
            Category category = categoryMapper.getCategoryById(postMapper.getCategoryIdByPostId(postList.get(i).getPostId()));

            postList.get(i).setCat(category);

            postList.get(i).setUser(userMapper.getUserById(postList.get(i).getPostUserId()));
        }
        return new PageInfo<>(postList);
    }

    public PageInfo<Post> pageSearchAllPost(Integer pageIndex, Integer pageSize, String search, String searchType) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Post> postList = new ArrayList<>();
        if (searchType.equals("username")) {
            User user = userMapper.getUserByUsername(search);
            if (user != null) {
                postList = postMapper.getPostsByUser(user);
            }
        }  else {
            postList = postMapper.getPostsByTitle("%" + search + "%");
        }
        for (int i = 0; i < postList.size(); i++) {
            //封装CategoryList
            Category category = categoryMapper.getCategoryById(postMapper.getCategoryIdByPostId(postList.get(i).getPostId()));

            postList.get(i).setCat(category);

            postList.get(i).setUser(userMapper.getUserById(postList.get(i).getPostUserId()));
        }
        return new PageInfo<>(postList);
    }

    public Post getPostByPostId(Integer commentPostId) {
        return postMapper.getPostByPostId(commentPostId);
    }

}
